import { ObjectUtil } from '../../utils/ObjectUtil';
export const AccountInfoService = {
	set: info => localStorage.setItem('AccountInfo', JSON.stringify(info)),
	remove: () => localStorage.removeItem('AccountInfo'),
	get: (str = '') =>
		ObjectUtil.fromJSON(str ? str : localStorage.getItem('AccountInfo') || ''),
	/**
	 * @author 戴俊明 <idaijunming@163.com>
	 * @description 账户的角色只要拥有要求中的一个即通过
	 * @description 账户的权限需要要拥有要求中的所有权限才能通过
	 * @date 2019/11/26 16:48
	 * @param roles 需要的角色
	 * @param accesses 需要的权限
	 * @param info 账户信息
	 **/
	isAllowed: (roles = [], accesses = [], info) => {
		info = info ? info : AccountInfoService.get();
		if (info) {
			const account_roles = info.roles ? info.roles : [];
			const account_perms = info.accesses ? info.accesses : [];
			if (roles.length > 0) {
				if (accesses.length > 0) {
					return (
						account_roles.some(item => roles.includes(item)) &&
						accesses.every(item => account_perms.includes(item))
					);
				} else {
					return account_roles.some(item => roles.includes(item));
				}
			} else if (accesses.length > 0) {
				return accesses.every(item => account_perms.includes(item));
			} else {
				return true;
			}
		} else {
			return false;
		}
	}
};
